<template>
    <el-dialog title="更 新" :visible.sync="udpateShow" :before-close="handleClose">
        <el-form ref="departmentFrom" :model="departmentFrom" label-width="135px" :rules="rules">
        <el-form-item label="部门编码" prop="departmentCode">
            <el-input v-model="departmentFrom.departmentCode"></el-input>
        </el-form-item>
        <el-form-item label="部门名称"  prop="departmentName">
            <el-input  v-model="departmentFrom.departmentName" ></el-input>
        </el-form-item>
         <el-form-item label="上级部门"  prop="parentId">
            <el-select v-model="departmentFrom.parentId" placeholder="请选择上级部门">
                <el-option value=-1 label="根"></el-option>
                <el-option v-for=" (item,key) of parentIds" :key="key" :value="item.id" :label="item.departmentName"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="备注"  prop="remark">
            <el-input v-model="departmentFrom.remark"></el-input>
        </el-form-item>
        <el-form-item label="部门负责人"  prop="personInCharge">
            <el-select v-model="departmentFrom.personInCharge" placeholder="请选择部门负责人" @change="selectPersonInCharge">
                <el-option value=0 label="无"></el-option>
                <el-option v-for=" (item,key) of staff" :key="key" :value="`${item.id}|${item.realName}|${item.phone}`" :label="item.realName+'-'+item.phone"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit('departmentFrom')">更 新</el-button>
            <el-button @click="uChange">取 消</el-button>
        </el-form-item>
        </el-form>
        <br><br>
        <p>注：先有部门才可添加部门人员，部门负责人只可以选本部门的人员</p>
    </el-dialog>
</template>
<script>
import { AllDepartmentList,GetDepartment,GetStaffByDepartment ,UpdateDepartment} from '@api/department/department'
  export default {
    data() {
      return {
          udpateShow: this.uShow,
          updateId:this.uId,
        departmentFrom: {
          id: '',
          departmentCode: '',
          departmentName: '',
          parentId:'',
          remark: '',
          personInChargeId:'',
          personInCharge:'',
          phone:''
        },
        parentIds:[],
        staff:[],
        rules: {
          departmentCode: [
                { required: true, message: '请输入部门编码', trigger: 'blur' },
                { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
           ],
           departmentName: [
                { required: true, message: '请输入部门名称', trigger: 'blur' },
                { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
            ]
        }
      }
    },
    /**双向绑定 start */
     props: ["uShow","uId"],
    watch: {
        uShow(val) {
            this.udpateShow = val;//②监听外部对props属性uShow的变更，并同步到组件内的data属性udpateShow中
        },
        udpateShow(val){
            //xxcanghai 小小沧海 博客园
             this.$emit("on-uShow-change",val);//③组件内对udpateShow变更后向外部发送事件通知
        }
        ,
         uId(val) {
            this.updateId = val;//②监听外部对props属性uShow的变更，并同步到组件内的data属性udpateShow中
            this.loadDepartment(val)
        },
        updateId(val){
            //xxcanghai 小小沧海 博客园
             this.$emit("on-id-change",val);//③组件内对udpateShow变更后向外部发送事件通知
        }
    },
  
    methods: {
        uChange() {
            this.udpateShow = !this.udpateShow;
        },
          /**双向绑定 end */

        onSubmit(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                this.loading = true
                UpdateDepartment(this.departmentFrom).then(res=>{
                    this.$message({
                    message: '更新成功',
                    type: 'success'
                    });
                     this.$emit("on-loadlist-change");//③组件内对myResult变更后向外部发送事件通知
                     this.loading = false
                     this.uChange()
                }).catch(err=>{
                    console.log('err', err)
                    this.loading = false
                })
               
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        handleClose(done){
            this.$confirm('确定关闭吗').then(() => {
                // function(done)，done 用于关闭 Dialog
                done();
            }).catch(() => {
               // console.log("点击确定时触发");
            });
        },
        fetchData () {
            this.loading = true
           AllDepartmentList().then(res => {
                // console.log(JSON.stringify(res.data))
                this.parentIds = res.data
                this.loading = false
            }).catch(err => {
                console.log('err', err)
                this.loading = false
            })
            
        },
        loadDepartment(id){
            this.loading = true
            GetDepartment({
                departmentId:id+""
            }).then(res=>{
                this.departmentFrom = res.data
                if(res.data.parentId == -1){
                    this.departmentFrom.parentId = res.data.parentId + ''
                }
                this.loading = false
            }).catch(err=>{
                console.log('err', err)
                this.loading = false
            })
            GetStaffByDepartment({departmentId:id}).then(res => {
                // console.log(JSON.stringify(res.data))
                this.staff = res.data
                this.loading = false
            }).catch(err => {
                console.log('err', err)
                this.loading = false
            })
        },
        selectPersonInCharge(){
            if (this.departmentFrom.personInCharge) {
                const [value,lable, phone] = this.departmentFrom.personInCharge.split('|')
                this.departmentFrom.personInChargeId = value
                this.departmentFrom.personInCharge = lable
                this.departmentFrom.phone=phone
            }
        }
    },
    created(){
        //console.log("created");
    },
    mounted(){
        //console.log("mounted");
        this.fetchData()
    },
    computed:{
       
    }
  }
</script>